/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hddz.comlib.ui.base.quicksearchbox.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.hddz.comlib.R;
import com.hddz.comlib.ui.base.quicksearchbox.Corpus;

/**
 * Adapter for showing a list of sources in the source selection activity.
 */
public class CorporaAdapter extends BaseAdapter {
	private static final String TAG = "CorporaAdapter";
	private static final boolean DBG = false;
	private List<Corpus> mRankedEnabledCorpora = new ArrayList<Corpus>();
	private CorpusViewInflater mCorpusViewInflater;

	public CorporaAdapter(Context context) {
		// mRankedEnabledCorpora.add(new SingleCorpus(context, "userName",
		// R.drawable.search_username, R.string.lbl_station_detail_username,
		// R.string.corpus_label_username_hint));
		// mRankedEnabledCorpora.add(new SingleCorpus(context, "userNo",
		// R.drawable.search_userno, R.string.lbl_station_detail_userno,
		// R.string.corpus_label_userno_hint));
		// mRankedEnabledCorpora.add(new SingleCorpus(context,
		// "waterMeterBodyNO", R.drawable.search_watermeterbody,
		// R.string.lbl_station_detail_watermterbody,
		// R.string.corpus_label_watermterbody_hint));
		// mRankedEnabledCorpora.add(new SingleCorpus(context, "phone",
		// R.drawable.search_phone, R.string.lbl_station_detail_phone,
		// R.string.corpus_label_phone_hint));
		// mRankedEnabledCorpora.add(new SingleCorpus(context, "installAddress",
		// R.drawable.search_address,
		// R.string.lbl_station_detail_installAddres, R.string.corpus_label_address_hint));
		mCorpusViewInflater = new CorpusViewInflater(context);
	}

	public int getCount() {
		return mRankedEnabledCorpora.size();
	}

	public Corpus getItem(int position) {
		return mRankedEnabledCorpora.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	/**
	 * Gets the position of the given corpus.
	 */
	public int getCorpusPosition(Corpus corpus) {
		if (corpus == null) {
			return 0;
		}
		int count = getCount();
		for (int i = 0; i < count; i++) {
			if (corpus.equals(getItem(i))) {
				return i;
			}
		}
		Log.w(TAG, "Corpus not in adapter: " + corpus);
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		CorpusView view = (CorpusView) convertView;
		if (view == null) {
			view = mCorpusViewInflater.createGridCorpusView(parent);
		}
		Corpus corpus = getItem(position);
		Drawable icon;
		CharSequence label;
		icon = corpus.getCorpusIcon();
		label = corpus.getLabel();
		if (DBG)
			Log.d(TAG, "Binding " + position + ", label=" + label);
		view.setIcon(icon);
		view.setLabel(label);
		return view;
	}
}
